//
//  commentViewController.m
//  百思不得姐comment
//
//  Created by 韦宗华 on 16/8/30.
//  Copyright © 2016年 joinWei. All rights reserved.
//

#import "commentViewController.h"
#import "commentModel.h"
#import "commentCell.h"
#import "BRCrossingTableViewCell.h"
@interface commentViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)NSArray* dataModel;
@property(nonatomic,strong)NSArray* hotmodel;
@property (weak, nonatomic) IBOutlet UITableView *commentTable;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomConstrain;

@property (weak, nonatomic) IBOutlet UIView *commentView;
@property (weak, nonatomic) IBOutlet UIButton *voiceBtn;
@property (weak, nonatomic) IBOutlet UITextField *commentLab;
@property(nonatomic,copy)NSString* urlStr;



@end

@implementation commentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tabBarController.tabBar.hidden = YES;

    self.urlStr =@"20263625";
    self.urlStr = self.model.ID;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.commentTable.estimatedRowHeight = 50;
    self.commentTable.rowHeight =UITableViewAutomaticDimension ;
    
    self.voiceBtn.hidden = YES;
    self.voiceBtn.layer.cornerRadius = 15;
    self.voiceBtn.layer.masksToBounds  = YES;
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(KeyboardDidChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
    
    
    [commentModel downloadWithUrlstring:[NSString stringWithFormat:@"http://api.budejie.com/api/api_open.php?a=dataList&appname=bs0315&asid=24320F1C-3569-4CAF-B69A-21608EE6758C&c=comment&client=iphone&data_id=%@&device=ios%%20device&from=ios&hot=1&jbk=0&mac=&market=&openudid=d41d8cd98f00b204e9800998ecf8427eb121ab44&page=1&per=50&udid=&ver=4.3",self.urlStr] successBlock:^(NSArray *arr, NSArray *hotArr) {
        self.dataModel = arr;
        self.hotmodel = hotArr;
        [self.commentTable reloadData];
    } failBlock:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    
    //UIView* xib
//    UINib *nib = [UINib nibWithNibName:@"BRCrossingTableViewCell" bundle:nil];
//    [[nib instantiateWithOwner:nil options:nil] lastObject];
//    
    BRCrossingTableViewCell* xib = [[[NSBundle mainBundle]loadNibNamed:@"BRCrossingTableViewCell" owner:nil options:nil]lastObject];
    
    xib.model = self.model;
    
    self.commentTable.tableHeaderView = xib;
    
    

}


- (void)viewDidAppear:(BOOL)animated{
   
    NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.commentTable selectRowAtIndexPath:index animated:YES scrollPosition:UITableViewScrollPositionTop];

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  
    [self.view endEditing:YES];
//    [tableView scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionTop animated:YES];
//
  
}

-(void)KeyboardDidChangeFrame:(NSNotification*)Notification
{
    NSDictionary* info = [Notification userInfo];
    
    CGRect frame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];
    
    float time = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey]floatValue];
  
   
    self.bottomConstrain.constant = self.view.frame.size.height-frame.origin.y;
    
    [UIView animateWithDuration:time animations:^{
        [self.view layoutIfNeeded];
    }];
    
    
    
    

    
}
- (IBAction)didClickVioceBtn:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected ==YES) {
        self.voiceBtn.hidden = NO;
        [self.commentLab resignFirstResponder];
    }
    else
    {
        self.voiceBtn.hidden = YES;
        [self.commentLab becomeFirstResponder];
    }
    
    
    
    
    
    
    
    
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    
    
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.view endEditing:YES];
    
}




-(NSArray *)dataModel
{
    if (!_dataModel) {
        _dataModel = [[NSArray alloc]init];
    }
    return _dataModel;
    
}
-(NSArray *)hotmodel
{
    if (!_hotmodel) {
        _hotmodel = [[NSArray alloc]init];
    }
    return _hotmodel;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (_hotmodel.count&&_dataModel.count) {
        return 2;
    }else if(_dataModel.count)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_hotmodel.count&&_dataModel.count) {
        if (section == 0) {
            return self.hotmodel.count;
        }else
        {
            return self.dataModel.count;
        }
    }else if(_dataModel.count)
    {
        return self.dataModel.count;
    }
    else
    {
        return 0;
    }
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    commentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"comment" forIndexPath:indexPath];
    if (self.hotmodel.count!=0 && indexPath.section == 0) {
        cell.commentmodel = self.hotmodel[indexPath.row];
    }
    else
    {
    cell.commentmodel = self.dataModel[indexPath.row];
    }
    
    
    return cell;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (_hotmodel.count&&_dataModel.count) {
        if (section ==0) {
            return @"最热评论";
        }
        else
        {
            return @"最新评论";
        }
        
    }else if(_dataModel.count)
    {
        return @"最新评论";
    }
    else
    {
        return nil;
    }
    
    
    
}

+(instancetype)commentViewController
{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"comment" bundle:nil];
    commentViewController *vc = [sb instantiateInitialViewController];
    
    return vc;
    
    
    
    
    
    
}


@end
